package com.talkweb.newproject.admin.rest;

import com.talkweb.newproject.admin.sysrole.dto.SysRoleDTO;
import com.talkweb.newproject.admin.sysrole.service.SysRoleService;
import com.talkweb.newproject.admin.sysrolemapping.service.SysRoleMappingService;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import com.talkweb.newproject.api.model.shiro.SysRole;
import com.talkweb.newproject.api.model.shiro.SysRoleParam;
import com.talkweb.newproject.common.utils.MapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qsf on 2019/4/23.
 */
@RestController
@RequestMapping(value="/api/sysRole")
@Slf4j
public class SysRoleRest {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private MapperUtil mapper;

    /**
     * 根据id查询role对象
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/get/v1/{id}",method = RequestMethod.POST)
    public ResponseResult<SysRoleDTO> querySysRoleByPrimaryKey(String id) throws Exception {
        ResponseResult<SysRoleDTO> result = new ResponseResult<>();
        try{
            SysRoleDTO sysRoleDTO = sysRoleService.querySysRoleByPrimaryKey(id);
            result.setStatus(Status.FAILD);
            result.setData(sysRoleDTO);
            result.setMessage("查询成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    @ResponseBody
    @RequestMapping(value="/getSysRoleList",method = RequestMethod.POST)
    public Object querySysRoleList(@RequestBody com.talkweb.newproject.api.model.admin.SysRoleParam dto) throws Exception {
        return sysRoleService.searchSysRole(dto);
    }

    /**
     * 根据条件查询role信息
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/queryLike",method = RequestMethod.POST)
    public ResponseResult<SysRoleDTO> queryLike(@RequestBody SysRoleDTO obj) throws Exception {
        ResponseResult<SysRoleDTO> result = new ResponseResult<>();
        try{
            SysRoleDTO dto = sysRoleService.queryLikeSysRole(obj);
            result.setStatus(Status.SUCCESS);
            result.setData(dto);
            result.setMessage("查询成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 新增数据
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/create/v1",method = RequestMethod.POST)
    public ResponseResult create(@RequestBody SysRoleDTO obj) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            sysRoleService.insertSysRole(obj);
            result.setStatus(Status.SUCCESS);
            result.setMessage("添加成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    /**
     * 根据id删除role对象
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/delete/v1",method = RequestMethod.POST)
    public ResponseResult deleteSysRoleById(@RequestParam("id") String id) throws Exception{
        ResponseResult result = new ResponseResult();
        try{
            boolean isDel = sysRoleService.deleteSysRole(id);
            if(isDel) {
                result.setStatus(Status.SUCCESS);
                result.setMessage("删除成功");
            }else {
                result.setStatus(Status.FAILD);
                result.setMessage("删除失败");
            }
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    /**
     * 修改role信息
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/update/v1",method = RequestMethod.POST)
    public ResponseResult update(@RequestBody SysRoleDTO obj) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            sysRoleService.updateSysRole(obj);
            result.setStatus(Status.SUCCESS);
            result.setMessage("修改成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    @RequestMapping(value="getSysRoleByUserId",method= RequestMethod.POST)
    public ResponseResult<SysRole> getSysRoleByUserId(@RequestBody SysRoleParam sysRoleParam) throws Exception {
        ResponseResult<SysRole> responseResult = new ResponseResult<>();
        try{
            List<SysRole> sysRoleList = new ArrayList<>();
            SysRoleDTO sysRoleDTO = new SysRoleDTO();
            sysRoleDTO.setTarget_id(sysRoleParam.getUserId());
            List<SysRoleDTO> sysRoleDTOList = sysRoleService.selectSysRoleByUserId(sysRoleDTO);
            sysRoleList=mapper.map(sysRoleDTOList,SysRole.class);
            responseResult.setStatus(Status.SUCCESS);
            responseResult.setDataList(sysRoleList);
            responseResult.setMessage("查询成功");
            return responseResult;
        }catch(Exception e) {
            log.error(e.getMessage());
            responseResult.setStatus(Status.FAILD);
            responseResult.setMessage("根据用户名查询角色信息错误,请重试!");
            return responseResult;
        }

    }

}
